# Set working directory to main replication folder
setwd("C:/Users/Eric/Dropbox/Book/analysis/replication/")

# Load packages
library(plyr)
library(dplyr)
library(ggplot2)
library(data.table)

# Read in data
d = fread("./data/wardata_book.csv")


#### FIGURE 0.1: The relative timing of negotiations across ninety-two interstate wars ####

# Create skeleton data
wns = unique(d$warName)
wns[which(wns=="IfniWar")] = "Ifni"
warNames = data.frame(warName=wns, offset=length(wns):1)

# Make some fixes to labels
dnegplot = d |> dplyr::select(warName, dayNum, propDone, propDoneR, neg)
dnegplot = join(dnegplot, warNames, by="warName")
dnegplot = dnegplot |> arrange(offset, dayNum)
dnegplot$warName[which(dnegplot$warName=="IfniWar")] = "Ifni"
dnegplot$negPlot = ifelse(dnegplot$neg==1, dnegplot$offset, -1000)

# Make the parts with the Russo-Japanese and Falklands War stand out
dnegplot$linecolor = ifelse(dnegplot$warName %in% c("Russo-Japanese", "Falkland Islands"), "focus", "others")

# Create plot
ggplot(dnegplot, aes(propDone, negPlot)) + geom_line(aes(color=linecolor, group=warName, size=linecolor)) + 
  scale_y_continuous("War", limits=c(1,92), breaks=1:92, minor_breaks = NULL, labels=rev(wns)) +  
  scale_x_continuous("Proportion of war elapsed") + 
  theme_bw() + 
  theme(legend.position = "none", 
        axis.text=element_text(size=5.5),
        axis.title.x = element_text(size = 9),
        axis.title.y = element_text(size = 9)) + 
  coord_cartesian(ylim=c(4,89)) +
  scale_color_manual(values=c("black", "gray60")) +
  scale_size_manual(values=c(1, 0.7)) 
  
# Export
ggsave("./figures/figure_0.1.pdf", height=7.5, width=4.5)